<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>列表自动排序</title>
</head>

<body>
    <p>点击按钮按字母列表排序:</p>
    <button onclick="sortList()">排序</button>
    <ul id="id01">
        <li>Oslo</li>
        <li>Stockholm</li>
        <li>Helsinki</li>
        <li>Berlin</li>
        <li>Rome</li>
        <li>Madrid</li>
    </ul>
    <script>
        let ul = document.getElementById('id01')
        let lis = document.getElementsByTagName('li')
        function sortList(){
            let arr = []
            Array.prototype.forEach.call(lis,(element)=>{
                arr.push(element.innerText)
            })
            arr.sort()
            for(let i=0;i<lis.length;i++){
                lis[i].innerHTML = arr[i]
            }
        }
    </script>
</body>

</html>